Skip to content

feat(firestore): literals pipeline stage#16028

Open
Linchin wants to merge 1 commit intogoogleapis:mainfrom
Linchin:fs-literal
Open

feat(firestore): literals pipeline stage#16028
Linchin wants to merge 1 commit intogoogleapis:mainfrom
Linchin:fs-literal

Conversation

@Linchin
Copy link
Contributor

@Linchin Linchin commented Mar 5, 2026

Succeeding googleapis/python-firestore#1170 for the monorepo migration.

@Linchin Linchin requested review from a team as code owners March 5, 2026 00:27
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Firestore client library by introducing a new literals pipeline stage. This stage provides a mechanism to inject a fixed set of predefined document objects directly into a pipeline, offering greater flexibility for testing individual pipeline stages and serving as initial inputs for complex operations like join conditions. The change includes the necessary class implementation, method integration into the base pipeline, and corresponding test coverage.

Highlights

  • New literals pipeline stage: Introduced a new literals method to the _BasePipeline class, allowing users to define documents from a fixed set of predefined objects. This stage is particularly useful for testing other pipeline stages in isolation or as inputs for join conditions.
  • Implementation of Literals class: Added the Literals class in pipeline_stages.py to represent the new pipeline stage, including its constructor and methods for protobuf serialization.
  • Comprehensive testing: Included new system-level and unit tests to ensure the correct functionality, behavior, and integration of the literals pipeline stage.
Changelog
  • packages/google-cloud-firestore/google/cloud/firestore_v1/base_pipeline.py
    • Added the literals method to _BasePipeline, enabling the definition of documents from a fixed set.
    • Included detailed docstrings with examples and behavior descriptions for the new literals method.
  • packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_stages.py
    • Introduced the Literals class, inheriting from Stage, to represent the new pipeline stage.
    • Implemented the __init__ and _pb_args methods for the Literals class to handle document arguments and their protobuf serialization.
  • packages/google-cloud-firestore/tests/system/pipeline_e2e/general.yaml
    • Appended a new system test case for the literals pipeline stage, demonstrating its usage with a sample document and asserting the expected results and protobuf structure.
  • packages/google-cloud-firestore/tests/unit/v1/test_pipeline.py
    • Updated the test_pipeline_methods parameterized test to include the new literals stage, ensuring it can be correctly appended to a pipeline.
  • packages/google-cloud-firestore/tests/unit/v1/test_pipeline_stages.py
    • Added a new test class TestLiterals with unit tests for the Literals stage's constructor, representation (repr), and protobuf serialization (_to_pb).
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@Linchin Linchin changed the title feat: literals pipeline stage feat(firestore): literals pipeline stage Mar 5, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new literals pipeline stage, which allows specifying a fixed set of documents as the starting point of a pipeline. The implementation includes the literals method on the pipeline builder, the Literals stage class, and corresponding unit and end-to-end tests. My review focuses on improving the clarity and correctness of the type hints and docstrings for the new functionality. I've suggested changes to make the API easier to understand and use correctly.

stages.FindNearest(field, vector, distance_measure, options)
)

def literals(self, *documents: str | Selectable) -> "_BasePipeline":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for *documents is incomplete. It should include dict as documents are often passed as dictionaries, which is not covered by str | Selectable.

Suggested change
def literals(self, *documents: str | Selectable) -> "_BasePipeline":
def literals(self, *documents: dict | str | Selectable) -> "_BasePipeline":

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably ignore this, unless that's how other languages handle it

Copy link
Contributor

@daniel-sanche daniel-sanche Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at go, it seems like it accepts maps, but not strings?

I don't know much about this stage, but from what I've seen, it's supposed to deal with maps. So maybe this should be def literals(self, *documents: Map | dict[str, CONSTANT_TYPE] | Selectable)?

Comment on lines +334 to +337
documents: A `str` or `Selectable` expression. If a `str`, it's
treated as a field path to an array of documents.
If a `Selectable`, it's usually a `Constant`
containing an array of documents (as dictionaries).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Args section of the docstring is misleading. It describes a single argument, but the method accepts *documents. It also incorrectly states that a str is treated as a field path; the implementation treats it as a string literal. To use a field path, field() should be used explicitly.

Suggested change
documents: A `str` or `Selectable` expression. If a `str`, it's
treated as a field path to an array of documents.
If a `Selectable`, it's usually a `Constant`
containing an array of documents (as dictionaries).
documents: One or more documents to be returned by this stage.
Each can be a `dict`, a `str` literal, or a
`Selectable` expression (e.g. `field("path")` or
`Constant.of(...)`).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Usually I'd put the * in here too: *documents:

class Literals(Stage):
"""Returns documents from a fixed set of predefined document objects."""

def __init__(self, *documents: str | Selectable):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for *documents is incomplete. It should include dict as documents are often passed as dictionaries, which is not covered by str | Selectable.

Suggested change
def __init__(self, *documents: str | Selectable):
def __init__(self, *documents: dict | str | Selectable):

Comment on lines +334 to +337
documents: A `str` or `Selectable` expression. If a `str`, it's
treated as a field path to an array of documents.
If a `Selectable`, it's usually a `Constant`
containing an array of documents (as dictionaries).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Usually I'd put the * in here too: *documents:

stages.FindNearest(field, vector, distance_measure, options)
)

def literals(self, *documents: str | Selectable) -> "_BasePipeline":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably ignore this, unless that's how other languages handle it

pipeline:
- Literals:
- title: "The Hitchhiker's Guide to the Galaxy"
author: "Douglas Adams"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's been a while since I looked at this, but this doesn't seem like the right syntax to me. Isn't this essentially sending Literals({"title": ""The Hitchhiker's Guide to the Galaxy", "author", "Douglas Adams"})? That doesn't seem to match the str | Selectable

Does the test pass?

... {"name": "alice", "age": 40}
... ]
>>> pipeline = client.pipeline()
... .literals(Constant.of(documents))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, it seems like:

  1. Constant isn't a Selectable
  2. Constant doesn't seem like it supports dict types. (We do have a Map, which serves that purpose, but it doesn't seem Selectable either)

stages.FindNearest(field, vector, distance_measure, options)
)

def literals(self, *documents: str | Selectable) -> "_BasePipeline":
Copy link
Contributor

@daniel-sanche daniel-sanche Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at go, it seems like it accepts maps, but not strings?

I don't know much about this stage, but from what I've seen, it's supposed to deal with maps. So maybe this should be def literals(self, *documents: Map | dict[str, CONSTANT_TYPE] | Selectable)?

val2 = Constant.of({"b": 2})
instance = self._make_one(val1, val2)
assert instance.documents == (val1, val2)
assert instance.name == "literals"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have tests that cover all supported input types. I don't see anything using str (and looking at go, I'm not sure if we should be supporting str?)

stringValue: "Douglas Adams"
title:
stringValue: "The Hitchhiker's Guide to the Galaxy"
name: literals No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have tests here that cover the different input types we support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants